
package simulator;
import javax.swing.*;

import pteam.PlanCActor;

import java.io.*;
import java.applet.Applet;
import java.net.URL;
/**
 * @deprecated 
 * This file allows one to embed the simulator in to a web page 
 * as a signed Applet. Note that this has been mostly replaced
 * in favor of a desktop runnable jar, and still links to 
 * GunnCS.org for resources
 */
public class ActorApplet extends JApplet {
  ByteArrayOutputStream boas = new ByteArrayOutputStream();
  PrintWriter out = new PrintWriter(boas, true);
  static Simulator sim = new Simulator();
  static String PREFIX = "http://gunncs.org/source/PTeam/src/";

  public String getOutput() {
    return boas.toString();
  }

  public void init() {
    try {
    
      Simulator.out = out;

      ClassLoader cl = Simulator.class.getClassLoader();

      Actor act = null;
      if (getParameter("actor") != null) {
        String classname = getParameter("actor");
        classname = classname.substring(0, classname.length() - ".java".length());
        System.out.println(classname);
        //out.println(classname);
        act = (Actor)cl.loadClass(classname).newInstance();
        
      } else {
        act = new PlanCActor();
        System.out.println("could not load actor");
      }
      sim.addActor(act);

      String maze = act.getMaze();
      sim.setEnvironment(EnvironmentFactory.loadConfiguration(
          new URL(PREFIX + maze + ".maze")));
      sim.robot = RobotFactory.getDefaultConfiguration(sim.env);
      
      add(sim);

      System.out.println("Running");
      
      new Thread(new Runnable() {
        public void run() {
          System.out.println("separate thread running");
          ActorApplet.sim.mainLoop();
        }
      }).start();
    } catch(Exception e) {
      e.printStackTrace(out);
    }
  }
  
  
}
